package com.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestHTTP {

	public boolean addCacert() {
		String command=" keytool -import -alias hqmststest -keystore \"%java_home%/jre/lib/security/cacerts\" -file d:/hqmststest.cer -storepass changeit ";
		Runtime rt = Runtime.getRuntime();
		try {
			Process pr = rt.exec(command);
			OutputStream os=pr.getOutputStream();
			BufferedReader br=new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String i=null;
			while( (i=br.readLine())!=null){
				System.out.println(i);
			}
			/*pr.getOutputStream().write("y".getBytes());
			pr.getOutputStream().flush();
			pr.getOutputStream().close();*/
			Process py =rt.exec("cmd.exe  y");
			BufferedReader by=new BufferedReader(new InputStreamReader(py.getInputStream()));
			String i2=null;
			while( (i2=br.readLine())!=null){
				System.out.println(i2);
			}
			//System.out.println("s=" + s);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		/*try {

			// Process
			// pr=rt.exec("keytool -import -alias hqmststest -keystore \"%java_home%/jre/lib/security/cacerts\" -file d:/hqmststest.cer -storepass changeit ");
			Process process = Runtime.getRuntime().exec(command);
			InputStreamReader ir = new InputStreamReader(
					process.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);
			String line;
			while ((line = input.readLine()) != null) {
				System.out.println(line);
			}
		} catch (java.io.IOException e) {
			System.err.println("IOException " + e.getMessage());
		}*/
		return true;
	}

	public void testHTTP() {
		try {
			String path="https://hqmststest.medidata.com.cn/docking/auto/gettoken.shtml?loginid=jiawei.cai&pwd=demo123123&datatype=json";
			URL url = new URL(path);
/*			HttpURLConnection uc = (HttpURLConnection) url.openConnection();
			// System.out.println(uc.);
			uc.connect();
			Object o = uc.getContent();
			System.out.println("o=" + o);
			String s = IOUtils.toString(uc.getInputStream());
			System.out.println("receive=" + s);
*/			
			HttpClient hc=new DefaultHttpClient();
			HttpGet hg=new HttpGet(path);
			HttpResponse hr=hc.execute(hg);
			InputStream is=	hr.getEntity().getContent();
			System.out.println(IOUtils.toString(is));
/*			HttpClient hc=HttpClient.New(url);
			hc.afterConnect();
			//hc.openServer("hqmststest.medidata.com.cn", url.getDefaultPort());
			InputStream is=hc.getInputStream();
			System.out.println(IOUtils.toString(is));
*/		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestHTTP th = new TestHTTP();
//		 th.addCacert();
		th.testHTTP();
	}

}
